package com.limao.Tool;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.limao.Model.ResultClass;

public class SMSAliyun {
	
	private static String host = "http://sms.market.alicloudapi.com";
	private static String path = "/singleSendSms";
	private static String method = "GET";
	private static String appcode = "184e1f64080a498bb7686a94b3166c03";
	private static String sin1="SMS_71170670";
	private static String sin2="七彩云购";

	public static boolean send(String mobile,String code)
	{
		boolean success=false;
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    String content="{\"code\":\""+code+"\"}";
	    querys.put("ParamString", content);
	    querys.put("RecNum", mobile);
	    querys.put("SignName", sin2);
	    querys.put("TemplateCode", sin1);
	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	String result=EntityUtils.toString(response.getEntity());
	    	System.out.println(result);
	    	ResultClass obj=JSON.parseObject(result,ResultClass.class);
	    	if(obj.success)
	    	{
				success = true;
	    	}
	    	else
	    	{
	    		success = false;
	    		System.out.println("阿里云短信发送失败 :"+obj.message);
	    	}
	    	
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return success;
		
	}

	
}
